﻿//////////////////////////////////////////////////////////////////////////////
// Copyright 2014 by Autodesk, Inc.  All rights reserved.
//
// This computer source code and related instructions and comments 
// are the unpublished confidential and proprietary information of 
// Autodesk, Inc. and are protected under applicable copyright and 
// trade secret law.  They may not be disclosed to, copied or used 
// by any third party without the prior written consent of Autodesk, Inc.
//////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using System.Collections.ObjectModel;

namespace Autodesk.AutoCAD.Windows.Features.PointCloud.PointCloudColorMapping
{
    public partial class ColorSchemeNameDlg : System.Windows.Window
    {
        public enum DialogType { Create, Rename };
        public ColorSchemeNameDlg(PointCloudMap data, DialogType type, string curName)
        {
            InitializeComponent();
            pointCloudMap = data;
            dialogType = type;

            if (dialogType == DialogType.Rename)
            {
                Title = Autodesk.AutoCAD.Windows.ResxResource.GetString("POINTCLOUD_RenameColorScheme");

                currentName = curName;
            }
            else if (dialogType == DialogType.Create)
            {
                Title = Autodesk.AutoCAD.Windows.ResxResource.GetString("POINTCLOUD_NewColorScheme");

                currentName = Autodesk.AutoCAD.Windows.ResxResource.GetString("POINTCLOUD_ColorScheme") + ' ';
                Int32 index = 0;
                while (pointCloudMap.HasSchemeName(currentName + index.ToString()))
                {
                    index++;
                }

                currentName += index.ToString();
            }

            SchemeName.Text = currentName;
            SchemeName.SelectAll();
            SchemeName.Focus();
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private const int chop = 25;
        private string PName(string newName)
        {
            newName =  newName.Length <= chop ? newName : newName.Substring(0, chop) + "...";
            newName = "\"" + newName + "\"";
            return newName;
        }
        private void OkBtn_Click(object sender, RoutedEventArgs e)
        {
            string newName = SchemeName.Text.Trim();

            Dictionary<string, string> values = new Dictionary<string, string>();
            values.Add("ObjectName", Autodesk.AutoCAD.Windows.ResxResource.GetString("POINTCLOUD_ColorScheme"));

            if (dialogType == DialogType.Rename &&
                newName.CompareTo(currentName) == 0)
            {
            }
            else if (pointCloudMap.HasSchemeName(newName))
            {
                values.Add("NewlyCreatedName", newName.Length <= chop ? newName : newName.Substring(0, chop) + "...");
                ShowTaskDialog(Internal.AcadTaskDialogs.StandardDuplicateNameError, values);
                return;
            }
            else if (newName.Length == 0)
            {
                ShowTaskDialog(Internal.AcadTaskDialogs.StandardInvalidNameEmptyName, values);
                return;
            }
            else if (newName.IndexOfAny(new char[] { '<', '>', '/', '\\', '"', ':', ';', '?', '*', '|', ',', '=', '`' }) != -1)
            {
                values.Add("FieldName", PName(newName));
                ShowTaskDialog(Internal.AcadTaskDialogs.StandardInvalidNameUnsupportedCharacters, values);
                return;
            }
            else if (newName.Length > 255)
            {
                values.Add("FieldName", PName(newName));
                ShowTaskDialog(Internal.AcadTaskDialogs.StandardInvalidNameTooLong, values);
                return;
            }

            DialogResult = true;
            NewName = newName;

            Close();
        }

        public string NewName
        {
            get { return newName; }
            set { newName = value; }
        }

        void ShowTaskDialog(string key, Dictionary<string, string> values)
        {
            Autodesk.Windows.TaskDialog taskDlg = Autodesk.Windows.TaskDialog.CreateFromFile(Internal.AcadTaskDialogs.Source(),
                                                                                                        key, values);
            System.Windows.Interop.WindowInteropHelper winInter = new System.Windows.Interop.WindowInteropHelper(this);
            taskDlg.Show(winInter.Handle);
        }

        private PointCloudMap pointCloudMap;
        private DialogType dialogType;
        private string currentName;
        private string newName;
    }
}
